/* allegro */
#include <allegro.h> 

/* standard includes */
#include <string.h>

/* our own includes */
#include "include/global.h"
#include "include/terrain.h"
#include "include/logger.h"
#include "include/loadpng.h"
#include "include/menu.h"
#include "include/display.h"
#include "include/cli.h"
 
/* lua */
#include <lua5.1/lua.h>
#include <lua5.1/lauxlib.h>
#include <lua5.1/lualib.h>

/* sdl_net */
#include <SDL/SDL_net.h>

lua_State *lua;

int do_setup(int argn, char * argv[], int * debug, int * hwmouse)
{
	if (allegro_init() != 0)
	{
		printf("\nCould not initialize allegro :-(\n\n");
		return 1;
	}

	// Parse the params
	int override_log = 0;
	int fullscreen = GFX_AUTODETECT_WINDOWED;
	for(int I = 0; I < argn; I++)
	{
		if(!strcmp(argv[I], "-l")) override_log = 1;
		else if(!strcmp(argv[I], "-f")) fullscreen = GFX_AUTODETECT_FULLSCREEN;
		else if(!strcmp(argv[I], "-d")) *debug = 0;
		else if(!strcmp(argv[I], "--help"))
		{
			printf("Command line options explained: \n\n");
			printf("-f\t- Fullscreen mode\n");
			printf("-l\t- Ignore it if the log can not be created\n");
			printf("-d\t- Remove debug overlay\n");
			printf("\n");
			return 1;
		}
	}

	// Start logging
	if(!strt_log("amazolog.txt") && override_log == 0)
	{
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to create a log file! Pass /l flag"
				" to force the app to ignore this");
		return 1;
	};	
	
	// Start lua
	lua = lua_open();
	luaL_openlibs(lua);
	
	install_timer(); 	
	
	/* 
	 * set a display mode before installing the mouse so we can test if HW 
	 * cursors are supported (else gfx_capabilities is not set)
	 *
	 * NOTE: call after installing timers, to keep track of fps
	 */
	display_on(640, 480, 16, fullscreen);
	
	install_keyboard();  
	install_mouse(); 	// must come after install_timer
	enable_hardware_cursor();
	
	/* generate a mouse cursor Lucasarts style */
	static BITMAP * cursor_bmp = 0;
	cursor_bmp = create_bitmap(32, 32);
   	clear_to_color(cursor_bmp, bitmap_mask_color(screen));
   	for (int i = 0; i<32; i++) {
   		if(abs(i - 16) < 3 || i < 6 || i > 32 - 6) continue;	// Empty in the center
      		putpixel(cursor_bmp, i, 16, makecol(255, 255, 255));
		putpixel(cursor_bmp, 16, i, makecol(255, 255, 255));
	}
	
   	/* select the custom cursor and set the focus point to the middle of it */
   	set_mouse_sprite(cursor_bmp);
   	set_mouse_sprite_focus(16, 16);

	
	show_mouse(screen);	/* <- should not go to the screen, if HARDWARE mouse is unavailable but fuck it */
	
	/* check if HW cursor is supported. Display mode has to be set first */
	if ((*hwmouse = ((gfx_capabilities & GFX_HW_CURSOR) == GFX_HW_CURSOR))) {

	} else {
		LOG_WARN("Hardware cursor NOT supported");
	}
	
	/* Make Allegro aware of PNG file format. */
    register_png_file_type();
 
 	/* Set the random seed */
 	srand(time(NULL));;
 	
 	/* Initialize SDL_net */
	if(SDLNet_Init() < 0) {
		amazolog("SDL_NET FAILURE: SDLNet_Init: %s\n", SDLNet_GetError());
		exit(1);
	}
	
	/* Initialize the CLI */
	am_init_cli();

	return 0;
}

void do_cleanup()
{
	am_term_cli();
	display_off();
	SDLNet_Quit();
	lua_close(lua);
	end_log();
}

int main(int argn, char * argv[])
{
	int debug = 1;
	int hwmouse = 0;
 	if(do_setup(argn, argv, &debug, &hwmouse)) {
 		/* TODO: possibly some resources are allocated before terminating. Free those */ 
 		return 1;
 	}
 	  
 	/* show the menu */
 	main_menu();
   	
   	/* do some snazzy cleanup job and get outta here */
   	do_cleanup();
   	return 0;
}
END_OF_MAIN()
